package SDK;

import java.io.*;
import java.net.HttpURLConnection;

import static SDK.HttpUtil.readInputStream;

public class Download {
    public static String downloadFile(String uuid){
        HttpURLConnection connection = HttpUtil.creatUrl("http://127.0.0.1:8080/file/download", "GET");
        connection.setRequestProperty("uuid",uuid);
        try {
            if(connection.getResponseCode()!=200){
                return "异常："+connection.getResponseCode();
            }else{
                String oldName= connection.getHeaderField("oldName");
                File f = new File("D:\\qiyuesuo\\download");
                if (!f.exists()){
                    f.mkdirs();
                }
                InputStream in= connection.getInputStream();
                byte[] b=new byte[1024];
                OutputStream out=new FileOutputStream("D:\\qiyuesuo\\download\\"+oldName);
                byte[] data = readInputStream(in);
                //写入数据
                out.write(data);
                //关闭输出流
                out.close();
                return "下载成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "下载失败";
        }
    }
}
